package com.cwt_taxi_xa.protocol.voice;

import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.cwt_taxi_xa.R;
import com.cwt_taxi_xa.util.TTsFunction;


/**
 * https://github.com/leavesC/FloatBall
 * Created by ZY on 2016/8/10.
 * 管理者，单例模式
 */
public class ViewManager{
    static String TAG = "ViewManager";

    private WindowManager windowManager;

    private LayoutParams floatBallParams;

    private Context context;

    private Handler mHandler = new Handler();

    private ProgressBar pgbUpgrade;
    private View root;
    private Button btnSuccess;
    private TextView tvAcousticsTitle;

    //私有化构造函数
    public ViewManager(Context context) {
        this.context = context.getApplicationContext();
        init();
    }

    public void init() {
        windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        root = View.inflate(context, R.layout.float_focal_acoustics, null);
        pgbUpgrade = (ProgressBar)root.findViewById(R.id.pgb_upgrade);
        btnSuccess = (Button) root.findViewById(R.id.btn_success);
        tvAcousticsTitle = (TextView) root.findViewById(R.id.tv_acoustics_title);
        btnSuccess.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hiddenFloatBall();
            }
        });
    }

    public void setMax(int max){
        if(pgbUpgrade != null){
            //pgbUpgrade.setMax(max);
        }
    }

    public void setProgress(int progress){
        if(pgbUpgrade != null){
            //pgbUpgrade.setProgress(progress);
        }
    }

    public void success() {
        tvAcousticsTitle.setText(R.string.window_text_upgrade_success);

        btnSuccess.setVisibility(View.VISIBLE);
        TTsFunction.speakText(context.getString(R.string.window_text_upgrade_success) + "请手动关闭升级窗口");
    }

    //显示浮动小球
    public void showFloatBall() {
        if (floatBallParams == null) {
            floatBallParams = new LayoutParams();
            floatBallParams.width = 500;
            floatBallParams.height = 200;
            floatBallParams.gravity = Gravity.CENTER;
            floatBallParams.type = LayoutParams.TYPE_SYSTEM_ALERT;
            //允许点击返回
            floatBallParams.flags = LayoutParams.FLAG_NOT_FOCUSABLE | LayoutParams.FLAG_NOT_TOUCH_MODAL | LayoutParams.FLAG_FULLSCREEN;
            floatBallParams.format = PixelFormat.RGBA_8888;
            tvAcousticsTitle.setText(R.string.window_text_title_upgrade);
            //btnSuccess.setVisibility(View.INVISIBLE);
            btnSuccess.setVisibility(View.VISIBLE);
            pgbUpgrade.setMax(100);
            pgbUpgrade.setProgress(0);
            windowManager.addView(root, floatBallParams);
        }
    }

    public void hiddenFloatBall(){
        if(floatBallParams != null){
            windowManager.removeView(root);
            floatBallParams = null;
        }
    }
}
